<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <!-- 判断矩阵是否是一个 X 矩阵 -->
    <script>
      function checkXMatrix(grid) {
        for (let i = 0; i < grid.length; i++) {
          for (let j = 0; j < grid[0].length; j++) {
            if ((i === j || i + j === grid.length - 1) !== !!grid[i][j]) return false
          }
        }
        return true
      }
      console.log(
        checkXMatrix([
          [2, 0, 0, 1],
          [0, 3, 1, 0],
          [0, 5, 2, 0],
          [4, 0, 0, 2]
        ])
      )
    </script>
  </body>
</html>
